package com.neuedu.nevserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.nevserver.dto.NodeInfoTypeDTO;
import com.neuedu.nevserver.pojo.NodeInfo;
import com.neuedu.nevserver.mapper.NodeInfoMapper;
import com.neuedu.nevserver.service.NodeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.nevserver.vo.NodeInfoTypeVO;
import com.neuedu.nevserver.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjy
 * @since 2025-03-18
 */
@Service
public class NodeInfoServiceImpl extends ServiceImpl<NodeInfoMapper, NodeInfo> implements NodeInfoService {

    @Autowired
    private NodeInfoMapper nodeInfoMapper;
    /**
     * 登录业务
     * @param code
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public ResultVO<NodeInfo> nodeLogin(String code, String password) throws Exception {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("code",code);
        NodeInfo nodeInfo=this.getOne(qw);
        //判断登录编码是否存在
        if(nodeInfo!=null){
            //判断密码是否正确
            if(nodeInfo.getPassword().equals(password)){
                //判断权限
                if(nodeInfo.getState()==2){
                    //返回当前节点的登录信息
                    return ResultVO.success("登录成功",nodeInfo);
                }
                return ResultVO.fail("权限不足，请联系管理员");
            }else{
                return ResultVO.fail("密码错误");
            }
        }
        return ResultVO.fail("账号不存在");
    }

    /**
     * 注册业务
     * @param nodeInfo
     * @return
     * @throws Exception
     */
    @Override
    public ResultVO nodeRegister(NodeInfo nodeInfo) throws Exception {
        nodeInfoMapper.insert(nodeInfo);
        return ResultVO.success("申请成功，请等待管理员审批");
    }

    @Override
    public ResultVO<List<NodeInfo>> listNode(NodeInfo nodeInfo) throws Exception {
        QueryWrapper qw=new QueryWrapper();
        if(nodeInfo.getNodeId()!=null){
            qw.eq("node_id",nodeInfo.getNodeId());
        }
        if(nodeInfo.getCity()!=null && !nodeInfo.getCity().equals("")){
            qw.eq("city",nodeInfo.getCity());
        }
        if(nodeInfo.getType()!=null){
            qw.eq("type",nodeInfo.getType());
        }
        if(nodeInfo.getState()!=null){
            qw.eq("state",nodeInfo.getState());
        }
        if(nodeInfo.getName()!=null && !nodeInfo.getName().equals("")){
            qw.eq("name",nodeInfo.getName());
        }
        return ResultVO.success(null,nodeInfoMapper.selectList(qw));
    }

    @Override
    public ResultVO<IPage<NodeInfoTypeVO>> pageNodeInfoType(NodeInfoTypeDTO nodeInfoTypeDTO) throws Exception {
        // 获取分页大小，如果为 null，则使用默认值 10
        int pageSize = (nodeInfoTypeDTO.getPageSize() != null) ? nodeInfoTypeDTO.getPageSize() : 10;
        IPage<NodeInfoTypeVO> page=new Page<>(pageSize,5);
        return ResultVO.success(null,nodeInfoMapper.pageNodeInfoAndType(page,nodeInfoTypeDTO));
    }
}
